package hz.mobileproject.pda.ui.activity;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import hz.mobileproject.pda.R;

/**
 * Created by zmybi on 2017/8/25.
 * 所有谈话人员播放音频界面
 */

public class AllTalkPlayMusicActivity extends AppCompatActivity {

    TextView pause;
    TextView start;
    TextView stop;
    boolean isPlaying=false;
    String filepath="http://192.168.1.130:8080/music";
    String nowMusic;
    int id=1;
    MediaPlayer mediaPlayer;
    SeekBar song_progress_normal;
    private Timer timer;
    private TimerTask task;


    private static final int UPDATE = 0;
    Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what)
            {
                case UPDATE:
                    song_progress_normal.setProgress(mediaPlayer.getCurrentPosition());
                    Log.i("mediaplayer",mediaPlayer.getCurrentPosition()+"");
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alltalk_playvideo);

        pause=(TextView)findViewById(R.id.pause);
        start=(TextView)findViewById(R.id.start);
        stop= (TextView)findViewById(R.id.stop);
        song_progress_normal= (SeekBar) findViewById(R.id.song_progress_normal);

        song_progress_normal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                int postion = seekBar.getProgress();
                mediaPlayer.seekTo(postion);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startMusic();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopMusic();
            }
        });

        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isPlaying=!isPlaying;
                System.out.println("1243544444444");
                if (isPlaying)
                {
                    pause.setText("暂停");
                    rePlayMusic();
                }else
                {
                    pause.setText("播放");
                    pauseMusic();
                }
            }
        });


    }



    private void startMusic() {
        nowMusic=filepath+id+".mp3";
        try {

            mediaPlayer = new MediaPlayer();

            mediaPlayer.setDataSource(nowMusic);//设置播放的数据源。
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            //mediaPlayer.prepare();//同步的准备方法。
            mediaPlayer.prepareAsync();//异步的准备
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                    start.setEnabled(false);

                    int max = mediaPlayer.getDuration();
                    Log.i("mediaplayer", max + "最大");
                    song_progress_normal.setMax(max);

                }
            });
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    start.setEnabled(true);
                }
            });

            new Thread(new Runnable() {
                @Override
                public void run() {

                    timer = new Timer();
                    task = new TimerTask() {
                        @Override
                        public void run() {
                            handler.sendEmptyMessage(UPDATE);
                        }
                    };
                    timer.schedule(task, 0, 5000);

                }
            }).start();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "播放失败", Toast.LENGTH_SHORT).show();
        }
    }


    private void pauseMusic() {

        if(mediaPlayer!=null)
            mediaPlayer.pause();

    }

    private void rePlayMusic()
    {
        if(mediaPlayer!=null)
            mediaPlayer.start();
    }
    public void stopMusic() {
        if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
            timer.cancel();
        }

        start.setEnabled(true);
    }
}
